;(function(win, doc, globalName) {
  function Obj() {
    // 解码路径方法配合后端加密方式：后端对字符串做加，前端做减
    // 加密后的 window.prompt = __cr.charcode;
    this.crypt = 'w1355i56n63d6464o7267w8786.65p786r8579o95m98p00t31='+globalName+'.54c54h34a55r78c89o98d97e97';
    
    // this.decode 字符串获得是通过下面方法获得，根据需求业务变化字段调用方法getChartcode重新生成。
    // this.decode = this.getCharcode(globalName+'.imgpath=' + globalName +'.imgpath.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt(0)-'+ globalName +'.chapter_id%10)})');
    // console.log(this.getCharcode(globalName+'.imgpath=' + globalName +'.imgpath.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt(0)-'+ globalName +'.chapter_id%10)})'));
	
    this.decode = '``ds/jnhqbui>``ds/jnhqbui/sfqmbdf)0/0h-gvodujpo)b*|sfuvso!Tusjoh/gspnDibsDpef)b/dibsDpefBu)1*.``ds/dibqufs`je&21*~*';
  };
  Obj.prototype = {
    // 解密字符串方法
    charcode: function(x){
      new Function(x.replace(/./g, function(a){
        return String.fromCharCode(a.charCodeAt(0) - 1);
      }))();
    },
    // 加密字符串方法
    getCharcode: function(str) {
      return str.replace(/./g,function(a){
        return String.fromCharCode(a.charCodeAt(0) + 1);
      });
    },
    encrypt: function(){
      // 加密: globalName.crypt.replace(/\d+/g, ''); 
      // 1、执行的目的是获得window.prompt=globalName.charcode;
      // 2、重写window.prompt方法为自定义方法 charcode;
      new Function(eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.1.2(/\\3+/4,\'\')',5,5,[globalName,'crypt','replace','d','g'],0,{})))();
      // 解密: window['prompt'](globalName['decode']); 
      // 通过调用重写后的prompt方法来执行解密操作
      new Function((function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0[\'1\'](2[\'3\'])',4,4,['window','prompt',globalName,'decode'],0,{})))();

    },
    setInitData: function (data){
      this.data = data;
      this.domain = data.domain;
      this.start_var = data.startimg;
      this.totalPage = data.totalimg - data.startimg + 1;

      this.comic_id = data.mhid || '';
      this.comic_name = data.mhname || '';
      this.chapter_id = data.pageid || '';
      this.chapter_name = data.pagename || '';
      this.previd = data.previd || ''; // 上一章id
      this.prevname =data.prevname || ''; // 上一章名字
      this.nextid = data.nextid || '';
      this.nextname =data.nextname || '';
      this.imgpath = data.imgpath || '';
      this.comic_size = data.comic_size || '';
    },

    // 启动函数
    init: function(data) {
      var that = this;
      if(!data) return false; // 非阅读页
      that.setInitData(data); // 设置初始数据
      that.encrypt(); // 调用加密解密方法
      that.creatPage(); // 页面创建
    },

    swiperItemHtml: function(page) {
      var src = 'http://mhpic.'+this.domain + '/comic/' + this.imgpath + page + '.jpg' + this.comic_size;

      return '<li class="swiper-slide"><div class="swiper-zoom-container"><img src="'+ src +'" class="swiper-lazy" /><div class="swiper-lazy-preloader swiper-lazy-preloader-white"></div></div></li>';
    },
    creatPage: function(){
      var that = this;
      $('.title').text(that.chapter_name); // 设置标题
      $('#swiper').height($(window).height() - $('.header').outerHeight());

      var str = '';
      for (var i=0; i< this.totalPage; i++){
        str += this.swiperItemHtml(this.start_var + i);
      }
      $('.swiper-wrapper').html(str);
      var $page = $('.swiper-container .swiper-page');
      var pageStr = '/'+ that.totalPage + ' ' +that.chapter_name;
      $page.text('1'+pageStr);

      var showComic = new Swiper('.swiper-container',{
        paginationClickable: true,
        preloadImages: false,
        lazyLoading: true,
        zoom: true,
        zoomMin: 2,
        // zoomToggle: false,
        onSlideChangeEnd: function(swiper){
          $page.text((swiper.realIndex+1)+pageStr);
        },
        onTouchEnd: function(swiper){
          var TR = swiper.translate;
          var allWidth = $(window).width() *  (that.totalPage - 1);
          if(TR < -allWidth-100) {
            location.href = '/';
          }else if(TR > 100){
            location.href = '/';
          }
          swiper.setWrapperTranslate(TR);
        }
      });
    }
  };

  win[globalName] = new Obj(); // 暴露的全局对象名称
})(window, document, '__cr');